package com.example.demo.service;

import com.example.demo.entity.Tag;
import com.example.demo.mapper.TagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class TagService {
    @Autowired
    private TagMapper tagMapper;

    // 获取或创建标签
    public List<Long> getOrCreateTags(List<String> tagNames) {
        List<Long> tagIds = new ArrayList<>();
        for (String tagName : tagNames) {
            // 查询标签是否存在
            Long tagId = tagMapper.selectTagIdByName(tagName);
            if (tagId == null) {
                // 标签不存在，创建新标签
                Tag tag = new Tag();
                tag.setName(tagName);
                tagMapper.insertTag(tag);
                tagId = tag.getId();
            }
            tagIds.add(tagId);
        }
        return tagIds;
    }
    //获取所有标签
    public List<Tag> getAllTags() {
        return tagMapper.selectAllTag();
    }
}
